using System;
using System.Collections.Generic;
using System.Text;
using MoEngine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MoGameLib
{
    public class BattleManager : CoreComponent
    {
        GroundTypeCollection m_GroundTypes = new GroundTypeCollection();
        public GroundTypeCollection GroundTypes
        {
            get { return m_GroundTypes; }
        }

        OverlayTypeCollection m_OverlayTypes = new OverlayTypeCollection();
        public OverlayTypeCollection OverlayTypes
        {
            get { return m_OverlayTypes; }
        }

        protected BattleMap m_CurrentMap;

        public BattleMap CurrentMap
        {
            get { return m_CurrentMap; }
        }


        private Battle m_CurrentBattle;

        public Battle CurrentBattle
        {
            get { return m_CurrentBattle; }
        }
        
        public virtual void CreateDemoBattle()
        {
            // create demo battle
            m_CurrentMap = new BattleMap();
            m_CurrentMap.Load("Content/Battles/Test.txt");

            Battle battle = m_CurrentBattle = new Battle(m_CurrentMap);

            // create some creatures/characters

            // create battlers

            BattleTeam team_pcs = battle.CreateTeam(MoCore.World.HumanPlayer);

            // a footman
            Creature footmanCrt = new Creature();
            footmanCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/Footman");
            Battler footman = battle.CreateBattler(footmanCrt, team_pcs, new Vector2Int(0, 1));

            // an assasin
            Creature assassinCrt = new Creature();
            assassinCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/Assassin");
            Battler assassin = battle.CreateBattler(assassinCrt, team_pcs, new Vector2Int(0, 2));
            
            // a druid
            Creature druidCrt = new Creature();
            druidCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/Druid");
            Battler druid = battle.CreateBattler(druidCrt, team_pcs, new Vector2Int(0, 3));

            // a paladin
            Creature paladinCrt = new Creature();
            paladinCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/Paladin");
            Battler paladin = battle.CreateBattler(paladinCrt, team_pcs, new Vector2Int(0, 4));
            
            // a dwarf
            Creature dwarfCrt = new Creature();
            dwarfCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/MountainKing");
            Battler dwarf = battle.CreateBattler(dwarfCrt, team_pcs, new Vector2Int(0, 5));

            BattleTeam team_monsters = battle.CreateTeam(null);

            // a orc
            Creature orcCrt = new Creature();
            orcCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/Grunt");
            Battler orc = battle.CreateBattler(orcCrt, team_monsters, new Vector2Int(7, 1));
            
            // a troll
            Creature trollCrt = new Creature();
            trollCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/ForestTroll");
            Battler troll = battle.CreateBattler(trollCrt, team_monsters, new Vector2Int(5, 2));

            // a bandit
            Creature banditCrt = new Creature();
            banditCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/BanditSpearThrower");
            Battler bandit = battle.CreateBattler(banditCrt, team_monsters, new Vector2Int(6, 3));

            // a bandit mage
            Creature banditMageCrt = new Creature();
            banditMageCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/BanditMage");
            Battler banditMage = battle.CreateBattler(banditMageCrt, team_monsters, new Vector2Int(6, 4));

            // a wolf rider
            Creature wolfRiderCrt = new Creature();
            wolfRiderCrt.BattleToken = Core.Content.Load<Texture2D>("Images/Battle/Tokens/ChaosWolfRider");
            Battler wolfRider = battle.CreateBattler(wolfRiderCrt, team_monsters, new Vector2Int(5, 5));



            // pawn like creation, deprecated!!!
#if false
            // Side A
            BattleSide curSide = BattleSide.Left;

            // a footman
            Creature crtFootman = new Creature();
            crtFootman.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/Footman");
            BattlePawn footman = new BattlePawn(crtFootman);
            footman.Side = curSide;
            battle.AddPawn(footman);

            // an assassin
            Creature crtAssassin = new Creature();
            crtAssassin.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/Assassin");
            BattlePawn assassin = new BattlePawn(crtAssassin);
            assassin.Side = curSide;
            battle.AddPawn(assassin);
            
            // a druid
            Creature crtDruid = new Creature();
            crtDruid.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/DruidOfTheTalon");
            BattlePawn druid = new BattlePawn(crtDruid);
            druid.Side = curSide;
            battle.AddPawn(druid);

            // a paladin
            Creature crtPaladin = new Creature();
            crtPaladin.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/Arthas");
            BattlePawn paladin = new BattlePawn(crtPaladin);
            paladin.Side = curSide;
            battle.AddPawn(paladin);

            // a dwarf
            Creature crtDwarf = new Creature();
            crtDwarf.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/HeroMountainKing");
            BattlePawn dwarf = new BattlePawn(crtDwarf);
            dwarf.Side = curSide;
            battle.AddPawn(dwarf);

            // Side B
            curSide = BattleSide.Right;

            // a orc
            Creature crtOrc = new Creature();
            crtOrc.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/ChaosGrunt");
            BattlePawn orc = new BattlePawn(crtOrc);
            orc.Side = curSide;
            battle.AddPawn(orc);

            // a troll
            Creature crtTroll = new Creature();
            crtTroll.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/ForestTroll");
            BattlePawn troll = new BattlePawn(crtTroll);
            troll.Side = curSide;
            battle.AddPawn(troll);

            // a bandit
            Creature crtBandit = new Creature();
            crtBandit.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/Bandit");
            BattlePawn bandit = new BattlePawn(crtBandit);
            bandit.Side = curSide;
            battle.AddPawn(bandit);

            // a BanditMage
            Creature crtBanditMage = new Creature();
            crtBanditMage.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/BanditMage");
            BattlePawn BanditMage = new BattlePawn(crtBanditMage);
            BanditMage.Side = curSide;
            battle.AddPawn(BanditMage);

            // a WolfRider
            Creature crtWolfRider = new Creature();
            crtWolfRider.BattleIcon = Core.Content.Load<Texture2D>("Images/Battle/Icons/ChaosWolfRider");
            BattlePawn WolfRider = new BattlePawn(crtWolfRider);
            WolfRider.Side = curSide;
            battle.AddPawn(WolfRider);
#endif
        }

        public override void Initialize()
        {
            base.Initialize();

            // create ground types
            BattleGroundType grass = m_GroundTypes.Create("g");
            grass.Layers = new BattleLayerInfo[1];
            grass.Layers[0].Image = Core.Content.Load<Texture2D>("Images/Battle/Ground/Grass");

            BattleGroundType brick = m_GroundTypes.Create("b");
            brick.Layers = new BattleLayerInfo[1];
            brick.Layers[0].Image = Core.Content.Load<Texture2D>("Images/Battle/Ground/Brick");
        }

        public override void Update(GameTime appTime)
        {
            if (CurrentBattle != null)
                CurrentBattle.Update(appTime);
            base.Update(appTime);
        }
    }
}
